using UnityEngine;
using System.Collections;

public class Sprite : MonoBehaviour {
		
	public static readonly float LAST_ANIMATE_TIME_INITIAL = -10000;
	public static readonly float TIME_PER_ANIMATION_FRAME = 0.042f;//默认24帧每秒 
	
	public int row_pics;
	public int col_pics;
	public int[] animation_frame;
	public float[] animation_spf;
	
	private float tiling_x;
	private float tiling_y;
	
	protected int next_animation_frame;
	private float last_animate_time;
	private float cur_animation_spf;
	private int cur_animation_start;
	private int cur_animation_end;
	private int cur_animate_step;
	private bool cur_animate_slide;
	private bool cur_animate_loop;
	private int cur_animate_play_times;
	//sprite正前方为X轴，正上方为Y轴
	//若flip_y == 1 角色局部X,Y,Z轴与世界坐标系对齐,若flip_y == -1 角色局部X,Z轴与世界坐标系相反
	protected int flip_y;
	// Use this for initialization
	private void Start () {
		last_animate_time = LAST_ANIMATE_TIME_INITIAL;
		flip_y = 1;
	}
	
	// Update is called once per frame
	private void Update () {
	
	}
	
	protected void Calculate_Tiling()
	{
		tiling_x = 1.0f/row_pics;
		tiling_y = 1.0f/col_pics;
		renderer.material.SetTextureScale("_MainTex", new Vector2(tiling_x, tiling_y));
	}
	
	//implement as a coroutine?????
	protected void Try_Play_One_Frame()
	{
		//播放间隔未达到 
		if (Time.time - last_animate_time < cur_animation_spf)	
			return;
		//播放间隔已达到，播放下一帧
		else
		{
			//重置上次播放时间 
			last_animate_time = Time.time;
			//检查下一帧是否有事件发生 
			Check_Frame_Event();
			//开始执行新动画帧 
			Play_One_Frame();
			
		}
			
	}
	
	protected void Play_One_Frame()
	{
		
			//动画超越结尾 
			bool exceed_end = next_animation_frame > cur_animation_end;
			if (exceed_end || next_animation_frame < cur_animation_start)
			{	
				//往回滑动播放 
				if (cur_animate_slide)
				{
					next_animation_frame = exceed_end ? cur_animation_end - 1 : cur_animation_start + 1;
					cur_animate_step = -cur_animate_step;
					if (cur_animate_loop)
					{
					}
					else
					{
						cur_animate_slide = cur_animate_play_times > 1 ? true : false;	
						cur_animate_play_times--;
					}
				}		
				//不滑动，循环或播放多次
				else if (cur_animate_loop || cur_animate_play_times > 1)
				{
					next_animation_frame = exceed_end ? cur_animation_start : cur_animation_end;
					if (!cur_animate_loop)
						cur_animate_play_times--;
				}
				//不滑动，不循环且播放完指定的次数
				else 
				{
					//动画播放结束 
					Animation_Finish();
					//return;
				}
			}
				
			renderer.material.mainTextureOffset = new Vector2((next_animation_frame % row_pics) * tiling_x,
		   												      (next_animation_frame / row_pics) * tiling_y);
				
			next_animation_frame += cur_animate_step;
	}
	
	virtual protected  void Check_Frame_Event()
	{
		
	}
	
	virtual protected void Animation_Finish()
	{
		
	}
	
	
	//设置动画
	//play_times <= 0:循环播放
	public void Play_Animation(int start_frame = 0 , int end_frame = 0 , float anim_spf = 0.042f , int play_times = 0 , bool random_pose = false , int anim_step = 1 , bool slide = false)
	{
		
		//anim_step>0:正向播放动画
		//anim_step<=0:·反向播放动画
		cur_animation_start = start_frame;
		cur_animation_end = end_frame;
		cur_animate_step = anim_step;
	
		//是否随机设置初始帧
		if (random_pose != true) 
			if (cur_animate_step > 0)
				next_animation_frame = cur_animation_start;
			else 
				next_animation_frame = cur_animation_end;
		else 
			next_animation_frame = Random.Range(start_frame,end_frame+1);
		
				
		//设置播放次数
		if (play_times > 0)
		{
			cur_animate_loop = false;
			if (slide)
				play_times = 2*play_times - 1;
		}
		else
			cur_animate_loop = true;

		cur_animate_play_times = play_times;
		cur_animate_slide = slide;
		cur_animation_spf = anim_spf;
		last_animate_time = LAST_ANIMATE_TIME_INITIAL;
	}
	
	//设置静止帧，并令物体静止
	public void Set_Motionless(int frame_to_stay = 0)
	{
		Play_Animation(frame_to_stay , frame_to_stay , 1000);
	}
	
	//左右方向镜像sprite
	protected void Reflect_LR()
	{
		
		flip_y = -flip_y;
		Mesh mesh = this.GetComponent<MeshFilter>().mesh;	
		int[] new_tri = new int[6];
		
		//更改顶点绘制顺序（翻转表面）
			new_tri[0] = 0;
			new_tri[1] = mesh.triangles[2];
			new_tri[2] = mesh.triangles[1];
			
			new_tri[3] = mesh.triangles[4];
			new_tri[4] = mesh.triangles[3];
			new_tri[5] = 1;

		mesh.triangles = new_tri;
		transform.Rotate(Vector3.up * 180 , Space.World);
		/*Debug.Log(mesh.triangles[0] );
			Debug.Log(mesh.triangles[1] );
			Debug.Log(mesh.triangles[2] );
			Debug.Log(mesh.triangles[3] );
			Debug.Log(mesh.triangles[4] );
			Debug.Log(mesh.triangles[5] );*/
	}
}
